package librarianPanels;

import java.awt.Dimension;
import clientGUI.MainGUI;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.DefaultTableModel;
import java.awt.Point;
import javax.swing.ListSelectionModel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ViewBookOrdersFrame extends JFrame /*PanelDesign*/ {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static JTable table;
	private String bid1;
	
	/**
	 * Create the panel.
	 */
	public ViewBookOrdersFrame(String name , String bid) 
	{
		setSize(new Dimension(384, 390));
		setLocation(new Point(300, 300));
		setPreferredSize(new Dimension(384, 390));
		setResizable(false);
		getContentPane().setLayout(null);
		setTitle(name + " Orders");
		bid1=bid;
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(0, 0, 379, 292);
		getContentPane().add(scrollPane);
		table = new JTable();
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.getTableHeader().setReorderingAllowed(false);
		table.setModel(new DefaultTableModel(
			new Object[][] {
				{null, null, null, null},
			},
			new String[] {
				"ID", "First Name", "Last Name", "Place"
			}
		));
		scrollPane.setViewportView(table);
		
		JButton btnNewButton_1 = new JButton("Cancel");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(table.getSelectedRow()!=-1)
				{
					String[] msgToServer=new String[4];
					msgToServer[0]="CancelOrder";
					msgToServer[1]=table.getModel().getValueAt(table.getSelectedRow(),0).toString();
					msgToServer[2]=bid1;
					msgToServer[3]=String.valueOf(table.getSelectedRow());
					MainGUI.OBLClient.handleMessageFromClient(msgToServer);
				}
				else 
				{
					JOptionPane.showMessageDialog(new JFrame(),	"ERROR - Nothing selected!","Error",JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnNewButton_1.setBounds(128, 305, 120, 25);
		getContentPane().add(btnNewButton_1);
		JButton btnNewButton = new JButton("View Orders");
		btnNewButton.setBounds(419, 388, 142, 45);
	}
	/**
	 * Replaces the data of the table in this panel with the given Matrix.
	 * @param data
	 */
	public static void changeTableContent(String[][] data)
	{
		table.setModel(new DefaultTableModel(data,		new String[] {
				"ID", "First Name", "Last Name", "Place"
			}){
			private static final long serialVersionUID = 1L;
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		});
		table.getTableHeader().setReorderingAllowed(false);
		table.getColumnModel().getColumn(0).setResizable(false);
		table.getColumnModel().getColumn(1).setResizable(false);
		table.getColumnModel().getColumn(2).setResizable(false);
		table.getColumnModel().getColumn(3).setResizable(false);
	}
}
